import { MoveToFloorCommand } from "../command/floor.command"
import CommandBinder from "../CommandBinder"
import { TourTransitionType } from "../enum/tourTransition.enum"
import viewmodeEnum from "../enum/viewmode.enum"
import { isPanOrMesh } from "../utils/viewmode.util"

export default class TourFloorChangeTransition {
  issueCommand: CommandBinder["issueCommand"]
  currentFloorId: () => string
  type: number
  toIndex: number
  currentTransitionPromise: Promise<void>
  onStopRequested: () => Promise<void>
  constructor(e, t) {
    this.issueCommand = e
    this.currentFloorId = t
    this.type = TourTransitionType.FloorChange
    this.toIndex = -1
    this.currentTransitionPromise = null
    this.onStopRequested = () => Promise.resolve()
  }
  get active() {
    return null !== this.currentTransitionPromise
  }
  get promise() {
    return this.currentTransitionPromise ? this.currentTransitionPromise : Promise.resolve()
  }
  async stop() {
    this.currentTransitionPromise && (await this.onStopRequested(), await this.promise, (this.currentTransitionPromise = null))
  }
  start(e) {
    if (this.active) throw Error("Transition already active")
    let t = Promise.resolve()
    const i = e.targetSnapshot.metadata.floorId
    const s = i !== this.currentFloorId()
    const n = e.targetSnapshot.metadata.cameraMode
    !isPanOrMesh(n) && n !== viewmodeEnum.Outdoor && s && (t = this.issueCommand(new MoveToFloorCommand(i, !0)))
    this.currentTransitionPromise = t.then(() => {
      this.currentTransitionPromise = null
    })
    return this
  }
}
